using System;
using System.Collections.Generic;
using System.Globalization;
using SF.Code.Extension.DebuggingHelper;
using SG.Ling.AutoTranslator.Common;

namespace SG.Ling.AutoTranslator.NLP
{
    public static class Support
    {
        private static readonly int[] LanguageToLCIDMapping;

        internal static readonly int NumberOfCategories = Enum.GetValues(typeof(Category)).Length;
        internal static readonly int NumberOfLanguages = Enum.GetValues(typeof (Language)).Length;
        public static readonly IDictionary<string, CultureInfo> Languages;

        static Support()
        {
            Languages = new Dictionary<string, CultureInfo>(NumberOfLanguages);
            LanguageToLCIDMapping = new int[NumberOfLanguages];

            Add(Language.Unknown, CultureInfo.InvariantCulture.LCID);
            Add(Language.English, 1033);
            Add(Language.German, 1031);
            Add(Language.Korean, 1042);
            Add(Language.Chinese, 2052);
            Add(Language.Spanish, 3082);
            Add(Language.French, 1036);
        }

        private static void Add(Language language, int lcid, params int[] subIds)
        {
            LanguageToLCIDMapping[(int)language] = lcid;
            Languages[language.ToString()] = CultureInfo.GetCultureInfo(lcid);  
        }

        public static CultureInfo ToCulture(this Language language)
        {
            try
            {
                return CultureInfo.GetCultureInfo(LanguageToLCIDMapping[(int)language]);
            }
            catch (Exception exception)
            {
                exception.Log(startIfNotAttached: true);
                return CultureInfo.InvariantCulture;
            }
        }
    }
}